package com.tms.app.tms.ui.home.history;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.tms.app.tms.R;
import com.tms.app.tms.adapter.HistoryAdapter;
import com.tms.app.tms.adapter.MyViewPagerAdapter;
import com.tms.app.tms.bean.TaskInfo;
import com.tms.app.tms.db.TaskInfoDBUtil;
import com.tms.app.tms.ui.HistrorySearchActivity;
import com.tms.app.tms.ui.base.ui.BaseListFragmnet;
import com.tms.app.tms.ui.base.view.IListView;
import com.tms.app.tms.ui.home.msg.OrderChildFt;
import com.tms.app.tms.utils.SharePreferenceUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by happy on 2017/8/26.
 */

public class TaskFt extends BaseListFragmnet implements IListView, View.OnClickListener {
    Unbinder unbinder;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.tab_layout)
    TabLayout tabLayout;
    @BindView(R.id.viewPager)
    ViewPager viewPager;
    @BindView(R.id.toolbar_right)
    TextView toolbarRight;
    @BindView(R.id.toolbar_search_button)
    TextView toolbarSearchButton;
    @BindView(R.id.toolbar_search_parent)
    LinearLayout toolbarSearchParent;
    @BindView(R.id.text_net_error)
    TextView textNetError;
    @BindView(R.id.swipeLayout)
    SwipeRefreshLayout swipeLayout;
    @BindView(R.id.toolbar_search_text)
    EditText toolbarSearchText;
    @BindView(R.id.img_net_error)
    ImageView imgNetError;
    @BindView(R.id.net_errot_layout)
    LinearLayout netErrotLayout;
    @BindView(R.id.toolbar_right_2)
    TextView toolbarRight2;
    private List<String> listTile = new ArrayList<>();
    private List<Fragment> fragmentList = new ArrayList<>();
    private HistoryP histtoryP;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.layout_fragment_task, null);
        unbinder = ButterKnife.bind(this, rootView);
        histtoryP = new HistoryP();
        initRecyclerView();
        setToolBarTitle(rootView, "历史任务", 0, R.mipmap.shousuo);
        toolbarSearchButton.setOnClickListener(this);
        histtoryP.setiView(this);
        initHistoryTask();
        return rootView;
    }

    @Override
    public void onStart() {
        super.onStart();
        hideLoadingDialog();
        histtoryP.getData(SharePreferenceUtil.getToken(getContext()), 1, "");
       // showLoadingDialog();
    }

    private void initRecyclerView() {
        adapter = new HistoryAdapter();
        adapter.setOnItemChildClickListener(histtoryP);
        adapter.setOnItemClickListener(histtoryP);
        LinearLayoutManager mgr = new LinearLayoutManager(getActivity());
        recyclerView.setLayoutManager(mgr);
        recyclerView.setNestedScrollingEnabled(false);
        recyclerView.setAdapter(adapter);
        swipeLayout.setOnRefreshListener(histtoryP);

    }

    private void  initHistoryTask(){
        List<TaskInfo> taskInfoList= TaskInfoDBUtil.getHistoryTaskList();
        if (taskInfoList!=null&&taskInfoList.size()>0)
        {
            adapter.getData().clear();
            adapter.getData().addAll(taskInfoList);
            adapter.notifyDataSetChanged();
            hideNerError();
        }
    }

    @OnClick({R.id.toolbar_right_2, R.id.toolbar_search_button, R.id.text_net_error})
    public void onClick(View view) {
        Intent intent = null;
        switch (view.getId()) {
            case R.id.toolbar_right_2:
                toolbarRight2.setVisibility(View.GONE);
                toolbarSearchParent.setVisibility(View.VISIBLE);
                break;
            case R.id.toolbar_search_button:
                toolbarRight2.setVisibility(View.VISIBLE);
                toolbarSearchParent.setVisibility(View.GONE);
                toolbarSearchText.setText("");
                intent = new Intent(getContext(), HistrorySearchActivity.class);
                intent.putExtra("SContent", toolbarSearchText.getText().toString());
                startActivity(intent);
                break;
            case R.id.text_net_error:
                showNetError();
                break;
        }

    }

    private void initView() {
        tabLayout.addTab(tabLayout.newTab().setText("全部"));
        tabLayout.addTab(tabLayout.newTab().setText("费用已确认"));
        tabLayout.addTab(tabLayout.newTab().setText("费用未确认"));
        listTile.add("全部");
        listTile.add("费用已确认");
        listTile.add("费用未确认");
        fragmentList.add(new OrderChildFt());
        fragmentList.add(new OrderChildFt());
        fragmentList.add(new OrderChildFt());
        MyViewPagerAdapter adapter = new MyViewPagerAdapter(getChildFragmentManager(), listTile, fragmentList);
        viewPager.setAdapter(adapter);
        tabLayout.setupWithViewPager(viewPager);
        //使用ViewPager的适配器
        tabLayout.setTabsFromPagerAdapter(adapter);
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }


    @Override
    public void pullMoreComlite(String... msg) {

    }

    @Override
    public void pullMoreNoMore(String... msg) {

    }

    @Override
    public void refreshComplite(String... msg) {
        super.refreshComplite(msg);
        hideNerError();
        swipeLayout.setRefreshing(false);
    }

    @Override
    public void refreshNoData(String... msg) {
        super.refreshNoData(msg);
        swipeLayout.setRefreshing(false);
    }

    @Override
    public void handlerErrorMsg(int errorCode) {

    }
}